package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //1、判断当前购物车是否已经存在要添加的商品
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart); //对象属性拷贝 dishId setmealId dishFlavor
        Long currentId = BaseContext.getCurrentId();  //拦截器解析到jwt令牌后将id存到 线程中共享的存储空间
        shoppingCart.setUserId(currentId); //设置 userId

        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        //2、如果已经存在 直接在商品数量上加一
        if (list != null && !list.isEmpty()) {
            ShoppingCart cart = list.get(0);      //因为通过用户id绑定 只能获得一条数据或者0条数据
            cart.setNumber(cart.getNumber() + 1); //在商品数量上加一
            shoppingCartMapper.updateNumber(cart);//更新到数据库中
        } else {
            //3、如果不存在 再添加到购物车中
            //判断本次添加到购物车的是 套餐还是菜品
            Long dishId = shoppingCartDTO.getDishId();
            Long setmealId = shoppingCartDTO.getSetmealId();
            if (dishId != null) {
                //本次添加的是菜品 - 根据菜品id查询菜品信息
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            } else {
                //本次添加的是套餐 - 根据套餐id查询套餐信息
                Setmeal setmeal = setmealMapper.getById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            //优化代码 - 统一插入 数量 创建时间
            shoppingCart.setNumber(StatusConstant.ENABLE);  //数量 = 1
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 查看购物车信息
     *
     * @return
     */
    @Override
    public List<ShoppingCart> showShoppingCart() {
        //获取当前微信用户的id
        Long currentId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(currentId)
                .build();
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        return list;
    }

    /**
     * 清空购物车 - 根据微信用户id
     */
    @Override
    public void cleanShoppingCart() {
        //获取当前微信用户的id
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(userId)
                .build();
        shoppingCartMapper.delete(shoppingCart);
    }

    /**
     * 删除购物车中一个商品
     *
     * @param shoppingCartDTO
     */
    @Override
    public void deleteShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart); //对象属性拷贝 dishId setmealId dishFlavor
        Long currentId = BaseContext.getCurrentId();  //拦截器解析到jwt令牌后将id存到 线程中共享的存储空间
        shoppingCart.setUserId(currentId); //设置 userId

        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        if (list != null && !list.isEmpty()) {
            ShoppingCart cart = list.get(0);      //因为通过用户id绑定 只能获得一条数据
            Integer number = cart.getNumber();
            //1、如果购物车中商品数量=1 则直接在数据库中删除
            if (number == 1) {
                shoppingCartMapper.delete(cart);
            } else {
                //2、如果购物车中商品数量>1 则直接在商品数量上减一
                cart.setNumber(cart.getNumber() - 1); //在商品数量上加一
                shoppingCartMapper.updateNumber(cart);//更新到数据库中
            }
        }
    }
}
